//
//  BonusSprite.cpp
//  airfight
//
//  Created by aaron on 12-11-22.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>
#include "BonusSprite.h"

BonusSprite::BonusSprite()
:m_pID(0)
{
    
}

bool BonusSprite::init()
{
    EntitySprite::init();
    m_iCollisionCount = 5;
    
    return true;
}

BonusSprite::~BonusSprite()
{
    //CCLog("destrory BONUS bullet address  = %0x , tag = %d",this,this->getTag());
    printf("BonusSprite\n");
}

void BonusSprite::onEnter()
{
    EntitySprite::onEnter();
    batch = (CCSpriteBatchNode*)this->getParent();
    screenSize = CCDirector::sharedDirector()->getWinSize();
    scheduleUpdate();
}

void BonusSprite::handleContact(ENTITY_TYPE type, CCDictionary *effect)
{
    switch (type) {
        case PLAYER_FIGHTER:
        {

            CCInteger* tag = (CCInteger*)effect->objectForKey("tag");
            CCDictionary *dict = CCDictionary::create();
            dict->setObject(tag, "tag");
            dict->setObject(CCInteger::create(BONUS_DESTROY_COLLECT), "type");
            CCNotificationCenter::sharedNotificationCenter()->postNotification(DESTROY_BONUS_NOTIFICATION, dict);
            break;
        }
        default:
        {
            break;
        }
    }
}

void BonusSprite::setRollingSpeed(int rollingSpeed)
{
    m_pRollingSpeed = rollingSpeed;
    double random(double,double);
    srand(unsigned(time(0)));
    float angle = (rand() % 360) * 2 * PI / 360;
    
    m_fSpeedX = m_pRollingSpeed * cos(angle);
    m_fSpeedY = m_pRollingSpeed * sin(angle);
}

void BonusSprite::update(float dt)
{
    if(this->getToDestroyFlag() != 0)
    {
        this->setVisible(false);
        b2Body *destroyBonusBody = this->getPhysicsBody();
        destroyBonusBody->SetActive(false);
        destroyBonusBody->GetWorld()->DestroyBody(destroyBonusBody);
        this->stopAllActions();
        switch (this->getToDestroyFlag()) {
            case BONUS_DESTROY_COLLECT:
            { 
               break;
            }
            case BONUS_DESTROY_OUT:
            {
                break;
            }
            default:
                break;
        }
        batch->removeChild(this, true);
        //CCLog("bonus countttt = %d",this->retainCount());
        delete this;
        return;
    }
    
    switch (m_pType)
    {
        case STRAIGHTDOWN:
        {
            this->setPositionY(this->getPositionY() + m_pRollingSpeed * dt);

            break;
        }
        case SIDECOLLIDE:
        {
            setPosition(ccpAdd(getPosition(), ccp(m_fSpeedX, m_fSpeedY)));
            if(m_iCollisionCount > 0)
            {
                if (getPositionX() < getContentSize().width/2 || getPositionX() > 320 - getContentSize().width/2 )
                {
                    m_fSpeedX = -m_fSpeedX;
                    m_iCollisionCount--;
                }
                
                if (getPositionY() < getContentSize().height/2 || getPositionY() > 480 - getContentSize().height/2 )
                {
                    m_fSpeedY = -m_fSpeedY;
                    m_iCollisionCount--;
                }
            }
                
            break;
        }   
        default:
            break;
    }
    
    if ((getPositionX() < 0 || getPositionX() > 320 || getPositionY() < 0 || getPositionY() > 480) && m_iCollisionCount <= 0)
    {
        CCInteger *tag = CCInteger::create(getTag());
        CCDictionary *dict = CCDictionary::create();
        dict->setObject(tag, "tag");
        dict->setObject(CCInteger::create(BONUS_DESTROY_OUT), "type");
        CCNotificationCenter::sharedNotificationCenter()->postNotification(DESTROY_BONUS_NOTIFICATION, dict);
    }
}
